package com.example.workbook.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.workbook.common.R;
import com.example.workbook.model.domain.Content;
import com.example.workbook.model.request.ContentRequest;
import com.example.workbook.service.ContentService;
import com.example.workbook.mapper.ContentMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author 53067
* @description 针对表【content】的数据库操作Service实现
* @createDate 2023-08-14 11:13:06
*/
@Service
public class ContentServiceImpl extends ServiceImpl<ContentMapper, Content>
    implements ContentService{
    @Autowired
    private ContentMapper contentMapper;
    @Override
    public R addContent(ContentRequest contentRequest) {

        Content content = new Content();
        contentRequest.setStart(new Date());
        BeanUtils.copyProperties(contentRequest,content);
        if (contentMapper.insert(content) > 0) {
            return R.success("添加成功");
        }else {
            return R.error("添加失败");
        }
    }

    @Override
    public R updateContentEndTime(ContentRequest contentRequest) {
        Content content = new Content();
        Integer id = contentRequest.getId();
        content.setId(id);
        content.setEnd(new Date());
        if (contentMapper.updateById(content) > 0) {
            return R.success("已成功结束任务");
        }else {
            return R.error("结束任务失败");
        }
    }
    @Override
    public R contentOfUserId(Integer userId) {
        LambdaQueryWrapper<Content> contentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        contentLambdaQueryWrapper.eq(Content::getUserId,userId);
        return R.success(null,contentMapper.selectList(contentLambdaQueryWrapper));
    }
}




